using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Enume;
using SO;
using Struct;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Serialization;

namespace Entity {

    public partial class EntityController {


        public List<FightTriggerItemData> FightSkillItemDatas = new List<FightTriggerItemData>();


        /// <summary>
        /// 添加技能
        /// </summary>
        /// <param name="skillId"></param>
        /// <param name="level"></param>
        public void AddFightSkill(int skillId, int level = 1) {
            SkillItemConfigSO configSo = SkillManager.Instance.GetConfigById(skillId);
            if (configSo.itemCheckTiming == EnumItemCheckTiming.AfterLearn) {
                List<PropertiesStruct> propertiesStructs = configSo.itemAdditionalAttributes;
                if (propertiesStructs.IsNullOrEmpty()) {
                    return;
                }
                propertiesStructs.ForEach(e => { GameLayer.Instance.PlayerController.FightProperties.AddProperty(e.propertiesType, e.value, e.isPercentage); });
            }

            FightTriggerItemData firstSkillData = FightSkillItemDatas.FirstOrDefault(x => x.id == skillId);
            if (firstSkillData != null) {
                firstSkillData.level += level;
                if (firstSkillData.level < 0) {
                    firstSkillData.level = 0;
                }
            } else {
                FightSkillItemDatas.Add(new FightTriggerItemData(skillId, level));
            }
        }

        /// <summary>
        /// 获取技能等级
        /// </summary>
        /// <param name="skillId"></param>
        /// <returns></returns>
        public int GetSkillLevel(int skillId) {
            FightTriggerItemData firstSkillData = FightSkillItemDatas.FirstOrDefault(x => x.id == skillId);
            if (firstSkillData != null) {
                return firstSkillData.level;
            } else {
                return 0;
            }
        }

        /// <summary>
        /// 根据技能检查时机获取技能数据
        /// </summary>
        /// <param name="itemCheckTiming"></param>
        /// <returns></returns>
        public List<(FightTriggerItemData, BaseTriggerItemSO)> GetFightSkillsBySkillCheckTiming(EnumItemCheckTiming itemCheckTiming) {
            List<(FightTriggerItemData, BaseTriggerItemSO)> skillDatas = new();
            foreach (FightTriggerItemData skillData in FightSkillItemDatas) {
                BaseTriggerItemSO itemConfigSo = SkillManager.Instance.GetConfigById(skillData.id);
                if (itemConfigSo.itemCheckTiming == itemCheckTiming) {
                    skillDatas.Add((skillData, itemConfigSo));
                }
            }

            return skillDatas;
        }

        /// <summary>
        /// 移除技能
        /// </summary>
        /// <param name="skillId"></param>
        public void RemoveFightSkill(int skillId) {
            FightTriggerItemData firstSkillData = FightSkillItemDatas.FirstOrDefault(x => x.id == skillId);
            if (firstSkillData != null) {
                FightSkillItemDatas.Remove(firstSkillData);
            }
        }

        /// <summary>
        /// 清空技能
        /// </summary>
        public void ClearFightSkills() {
            FightSkillItemDatas.Clear();
        }

        /// <summary>
        /// 技能是否存在
        /// </summary>
        /// <param name="skillId"></param>
        /// <returns></returns>
        public bool SkillIsExist(int skillId, out int skillLevel) {
            skillLevel = GetSkillLevel(skillId);
            return skillLevel > 0;
        }


    }

}